<?php

class Route
{
	public $module;
	public $controllername;
	public $actionname;
	public $redirect_url=null;
	public static $instance=null;
	public  function __construct()
	{
		if(self::$instance!==null)
		{
			throw new Exception("Route is created!");
		}
		$this->getRedirectUrl();
		
	}

	public static  function getInstance()
	{
		if(self::$instance===null)
		{
			$class=__CLASS__;
			self::$instance=new $class();
		}
		return self::$instance;
	}
	
	//return not include Controller  etc:Index not IndexController
	public function getControllerName()
	{
		if(empty($this->controllername))
		{
			$module=$this->getModuleName();
			if(!@empty($_GET["c"]))
			{
				$controller_name=$_GET["c"];				
			}
			else if(!empty($this->redirect_url[0]))
			{
				$controller_name=$this->redirect_url[0];
				unset($this->redirect_url[0]);
				$temp_redirect=array();
				if(!empty($this->redirect_url))
				{
					foreach ($this->redirect_url as $url)
					{
						$temp_redirect[]=$url;
					}
					$this->redirect_url=$temp_redirect;
				}
			}
			else 
			{
				$controller_name="index";
			}
			$controller_name=strtolower($controller_name);
			$controller_name=ucfirst($controller_name);
			$controller_name=basename($controller_name,URL_SUFFIX);
			$this->controllername=$controller_name;
		}
		return $this->controllername;
	}

	//return not include Action  etc:index not indexAction
	public function getActionName()
	{
		if(empty($this->actionname))
		{
			$controller_name=$this->getControllerName();
			if(!@empty($_GET["a"]))
			{
				$action_name=$_GET["a"];
			}
			else if(!empty($this->redirect_url[0]))
			{
				$action_name=$this->redirect_url[0];
			}
			else 
			{
				$action_name="index";
			}
			$action_name=basename($action_name,URL_SUFFIX);
			$this->actionname=$action_name;
		}
		return $this->actionname;
	}
	public function getModuleName()
	{
		if(empty($this->module))
		{
			$registry=Registry::getInstance();
			$frontcontroller=$registry->get("frontcontroller");
			$modules=$frontcontroller->getRootDir();
			$modules=$modules."application/modules/";
			$modules=scandir($modules);
			$temp_modules=array();
			for ($i=count($modules)-1;$i>=0;$i--)
			{
				if($modules[$i]!="."&&$modules[$i]!="..")
				{
					$temp_modules[]=$modules[$i];
					$temp_modules[$modules[$i]]=$modules[$i];
				}
			}
			$modules=$temp_modules;
			unset($temp_modules);
			
			if(!@empty($_GET["m"])&&@key_exists($_GET["m"],$modules))
			{
				$this->module=$modules[$_GET["m"]];
			}
			else if(!empty($this->redirect_url[0])&&key_exists($this->redirect_url[0],$modules))
			{
				$this->module=$this->redirect_url[0];
				
				unset($this->redirect_url[0]);
				$temp_redirect=array();
				if(!empty($this->redirect_url))
				{
					foreach ($this->redirect_url as $url)
					{
						$temp_redirect[]=$url;
					}
					$this->redirect_url=$temp_redirect;
				}
			}
			else 
			{
				$this->module="default";
			}
		}
		
		return $this->module;
	}

	public function getRedirectUrl()
	{
		if($this->redirect_url===null)
		{
			$registry=Registry::getInstance();
			$frontcontroller=$registry->get("frontcontroller");

			$redirect_url=$_SERVER['REQUEST_URI'];
			$base_url=$frontcontroller->getBaseUrl();
			
			if(strlen($redirect_url)<=strlen($base_url))
			{
				$this->redirect_url=array();
				return $this->redirect_url;
			}
			$redirect_url=substr($redirect_url,strlen($base_url));
			$query_url=strpos($redirect_url,"?");
			if(is_int($query_url))
			{
				$redirect_url=substr($redirect_url,0,$query_url);
			}
			$this->redirect_url=explode("/",$redirect_url);
		}
		
		return $this->redirect_url;
	}
}